# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=mesa
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=11.2.2
pkgrel=1
pkgdesc="Open-source implementation of the OpenGL specification (mingw-w64)"
arch=('any')
depends=("")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "scons"
             "libxml2-python"
             "python2-mako")
url="http://www.mesa3d.org"
license=('MIT')
options=('staticlibs' 'strip')
source=(ftp://ftp.freedesktop.org/pub/mesa/${pkgver}/${_realname}-${pkgver}.tar.xz{,.sig}
        1b62b47f6fcaf5a825a5a909c91e802bffae3b1d.diff
        c521f2d7373a241a69238b95a8c0f3725a9da5ad.diff
        001-extra-libs.patch)
sha256sums=('40e148812388ec7c6d7b6657d5a16e2e8dabba8b97ddfceea5197947647bdfb4'
            'SKIP'
            '413ce447a028a7691f4b4f25431414bd05f2fc909dd5af1a747103572a4704a2'
            '48c04d1643e411274ff6e9218edfac1011e8933fabd136c4e42f5daafddb171d'
            'bc9bb5013ac80ded47ad164ae1ef58cc9a39784eb4bf61e8c7d654bb273b05a9')
validpgpkeys=('8703B6700E7EE06D7A39B8D6EDAE37B02CEB490D')

case ${MINGW_CHOST} in
  i686*)
    _mach=x86
  ;;
  x86_64*)
    _mach=x86_64
  ;;
esac

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  # These two patches are needed for SCONS 2.5
  patch -p1 -i ${srcdir}/c521f2d7373a241a69238b95a8c0f3725a9da5ad.diff
  patch -p1 -i ${srcdir}/1b62b47f6fcaf5a825a5a909c91e802bffae3b1d.diff
  patch -p1 -i ${srcdir}/001-extra-libs.patch
}

build() {
  cd ${srcdir}/${_realname}-${pkgver}

  LLVM=${MINGW_PREFIX} \
  CFLAGS="-march=core2 -pipe -D_USE_MATH_DEFINES" \
  CXXFLAGS="-march=core2 -pipe -std=c++11 -D_USE_MATH_DEFINES" \
  LDFLAGS="-static -s" \
  scons build=release platform=windows toolchain=mingw machine=${_mach} libgl-gdi #mesagdi
}

package() {
  cd ${srcdir}/${_realname}-${pkgver}

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin
  mkdir -p ${pkgdir}${MINGW_PREFIX}/lib/mesa
  local _dll_files=( $(find ${srcdir}/${_realname}-${pkgver}/build/windows-${_mach} -type f -name "*.dll") )
  cp -f ${_dll_files[@]} ${pkgdir}${MINGW_PREFIX}/bin/
  local _dlla_files=( $(find ${srcdir}/${_realname}-${pkgver}/build/windows-${_mach} -type f -name "*.a") )
  cp -f ${_dlla_files[@]} ${pkgdir}${MINGW_PREFIX}/lib/mesa/
}
